//
//  LyTabBarController.swift
//  SinaWeibo
//
//  Created by 刘莹 on 2017/5/18.
//  Copyright © 2017年 liuying. All rights reserved.
//

import UIKit

class LyTabBarController: UITabBarController {

    override func viewDidLoad() {
        
        //将UITabBarController的属性  tabBar替换成自定义的LyMainTabBar
        //只读属性不能直接赋值值,要通过kvc
        //self.tabBar = LyMainTabBar()
        let lytabBar = LyMainTabBar()
        self.setValue(lytabBar, forKey: "tabBar")
        //实现闭包
        lytabBar.composeClousre = { [weak self] in
        
            print("撰写按钮被点击了",self as Any)
           
        }
        

        
        //添加子视图控制器
        addNavs()
        
        
        
    }
    
    //MARK: -
    //MARK: - 添加nav
   private func addNavs(){
    
        addChildVC(vc: LyHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildVC(vc: LyMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildVC(vc: LyDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildVC(vc: LyProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //MARK: -
    //MARK: - 添加控制器
   private func addChildVC(vc:UIViewController,title:String,imageName:String){
    
        //设置Nav的title
        vc.navigationItem.title = title
        
        //设置tabBar的title
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //选中图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        
        //addChildViewController:如果在自定义TabBar:将tabBar上的tabBarButton隐藏,而是自定义按钮,并且使用这个方法;就会出现自定义按钮无法点击,解决:使用setViewControllers:animated
//        let  nav = UINavigationController(rootViewController: vc)
        let nav = LyBaseNavigationController(rootViewController: vc)
        addChildViewController(nav)

    }
    
    
}
